<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越小越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入名称"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="选择分类">
                    <div class="w-[800px] display">
                        <el-select class="w-[280px]" v-model="formData.class"  placeholder="请选择轮播图分类">
                            <el-option
                                v-for="(item, key) in pager.lists"
                                :key="key"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                        <!-- <el-input v-model="formData.pc_plate" placeholder="请输入pc网站模板" show-word-limit /> -->
                    </div>
                </el-form-item>
                <el-form-item label="图片" prop="img">
                    <material-picker
                        v-model="formData.img"
                        :limit="1"
                    />
                </el-form-item>
                <el-form-item label="跳转链接" prop="link">
                    <el-input
                        v-model="formData.link"
                        placeholder="请输入跳转链接"
                        clearable
                    />
                </el-form-item>
                <el-form-item  label="显示" prop="is_display">
                    <div class="w-80">
                                <el-radio-group
                                    v-model="
                                        formData.is_display
                                    "
                                >
                                    <el-radio :label="1">显示</el-radio>
                                    <el-radio :label="2">隐藏</el-radio>
                                </el-radio-group>
                            </div>
                </el-form-item >
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import Popup from '@/components/popup/index.vue'
import type { FormInstance } from 'element-plus'
import { usePaging } from '@/hooks/usePaging'
import {  setChart,getDetilsChart,getClass } from '@/api/application/website/swiper'
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑轮播图' : '新增轮播图'
})
const formData = reactive({
    id:0,
    name:"",
    class:"",
    img:"",
    link:"",
    sort:"",
    is_display:"",
})
const results = ref([])
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const handleSubmit = async () => {
    await formRef.value?.validate()
    await setChart(formData)
    popupRef.value?.close()
    emit('success')
}
const getDetail = async (row: Record<string, any>) => {
    const data = await getDetilsChart({
        id: row.id
    })
    setFormData(data)
}
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const handleClose = () => {
    emit('close')
}
const { pager, getLists } = usePaging({
    fetchFun: getClass,
    params: formData
})
getLists()
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
